package org.pub.app.lunch;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class LunchSelector extends Activity {
	private final String TAG = "LunchSelector";
	private Button gpsButton = null;
	private Button addrButton = null;
	private Button airSearchBtn = null;
	private Button btnTest = null;
	// private TextView gpsText = null;
	// private boolean isLoadGps = false;
	private LocationManager locationManager;
	private Context context;
	private String provider;

	private GpsLocationListener listener = null;

	// private final static int SLEEP_TIME = 3000;
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.context = this.getBaseContext();
		setContentView(R.layout.lunchemain);

		loadGps();

		// new Thread( timerThread ).start();
		gpsButton = (Button) findViewById(R.id.btn_gps_search);
		addrButton = (Button) findViewById(R.id.btn_addr_search);
		airSearchBtn = (Button) findViewById(R.id.btnAirSearch);
		btnTest = (Button) findViewById(R.id.btnTest);

		gpsButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Log.w(TAG, "onStatusChanged");
				Intent in = new Intent(context, RestaurantList.class);
				Location loc = null;

				loc = getLocation();

				if (loc == null) {
					Log.w(TAG, "location is null");

					AlertDialog.Builder adb = new AlertDialog.Builder(LunchSelector.this);
					adb.setPositiveButton("OK", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
						}
					});
					adb.setTitle(R.string.alert_title);
					adb.setMessage(R.string.alert_message);
					adb.show();
				} else {
					in.putExtra("MY CURRENT LOCATION", new Location(loc));
					startActivity(in);
				}
			}

			private Location getLocation() {

				Location location = locationManager.getLastKnownLocation(provider);
				if (location == null) {
					Log.w(TAG, "get Location From GPS Fail !!!!!");
					location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
				}
				return location;
			}
		});

		addrButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				// 주소 입력 화면으로 옮겨주자..
				Intent in = new Intent(context, InputAddress.class);
				startActivity(in);
			}
		});

		airSearchBtn.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				Intent in = new Intent(context, SearchForm.class);
				startActivity(in);
			}
		});

		btnTest.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				//Intent in = new Intent(context, LooperTest.class);
				Intent in = new Intent(context, Loading.class);
				startActivity(in);

			}
		});

	}

	// Handler msgHandle = new Handler( ){
	// public void handleMessage(Message message) {
	// switch( message.what ){
	// case R.id.message_timer_end:
	// alertUsingGps();
	// break;
	// }
	// }
	// };
	//
	// /**
	// *
	// */
	// Runnable timerThread = new Runnable(){
	// public void run(){
	// try{
	// Thread.sleep( SLEEP_TIME );
	// msgHandle.sendEmptyMessage( R.id.message_timer_end );
	// }catch( Exception e ){
	// Log.w( TAG , e );
	// }
	// }
	// };
	//
	// public void alertUsingGps() {
	//
	// AlertDialog.Builder adb = new AlertDialog.Builder(this);
	// adb.setPositiveButton("OK", new DialogInterface.OnClickListener() {
	//
	// // @Override
	// public void onClick(DialogInterface dialog, int which) {
	// loadGps();
	// }
	// });
	// adb.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
	// // @Override
	// public void onClick(DialogInterface dialog, int which) {
	// }
	// });
	// adb.setTitle( R.string.alert_title );
	// adb.setMessage(R.string.alert_message);
	// adb.show();
	// }

	public void loadGps() {
		Log.w(TAG, "loadGps");
		locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		// locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
		// 2000, 10, new GpsLocationListener());
		// isLoadGps = true;

		Criteria criteria = new Criteria();
		criteria.setAccuracy(Criteria.ACCURACY_FINE); // 정확도
		criteria.setPowerRequirement(Criteria.POWER_LOW); // 전원 소비량
		criteria.setAltitudeRequired(false); // 고도, 높이 값을 얻어 올지를 결정
		criteria.setBearingRequired(false);
		criteria.setSpeedRequired(false); // 속도
		criteria.setCostAllowed(true); // 위치 정보를 얻어 오는데 들어가는 금전적 비용
		provider = locationManager.getBestProvider(criteria, true);
		listener = new GpsLocationListener();
		locationManager.requestLocationUpdates(provider, 1000, 5, listener);
	}

	private class GpsLocationListener implements LocationListener {

		public void onLocationChanged(Location location) {
			Log.w(TAG, "onLocationChanged");
		}

		public void onProviderDisabled(String provider) {
			Log.w(TAG, "onProviderDisabled");
		}

		public void onProviderEnabled(String provider) {
			Log.w(TAG, "onProviderEnabled");
		}

		public void onStatusChanged(String provider, int status, Bundle extras) {
			Log.w(TAG, "onStatusChanged");
		}
	}

	protected void onStart() {
		super.onStart();
		if (listener == null) {
			listener = new GpsLocationListener();
			locationManager.requestLocationUpdates(provider, 1000, 5, listener);
		}
	}

	protected void onStop() {
		super.onStop();
		locationManager.removeUpdates(listener);
		listener = null;
	}
}